/**
 * Created by wzh on 2016/5/27.
 */
'use strict';

angular.module('swanPc')
    .controller('NavbarController', function ($rootScope,$scope, $location, $state, Auth,Account,GetAccountInfo,newMessageManage,GetOrganizationInfo,$cookieStore) {
        // $scope.returnIsAuthenticated = function(){
        //     $scope.isAuthenticated = Auth.isLoggedIn;
        //     return  $scope.isAuthenticated;
        // };
        $scope.isAuthenticated = Auth.isLoggedIn;

        $scope.logout=Auth.logout;
        $scope.photoUrl = null;
        $scope.getPresentUser = function(){
            console.log("GET");
            return $cookieStore.get("_user");
        };


        $scope.$watch(function(){
            return $scope.isAuthenticated();
        },function(newV,oldV,scope){
            // console.log($scope.isAuthenticated());
            $scope.MessageNoReply=0;
            if(newV){
                GetAccountInfo.get(function(result){

                        // console.log(result);
                        // console.log($cookieStore.get("_user"));
                        $rootScope.presentAccountInfo=result;
                        $scope.presentAccountInfo=result;
                        $scope.log_username=$scope.presentAccountInfo.firstName;
                        if($scope.presentAccountInfo.photoUrl!=null){
                            $scope.photoUrl = $rootScope.photoPrefixString + $scope.presentAccountInfo.photoUrl;
                        }else{
                            $scope.photoUrl = "assets/img/avatars/avatar3.jpg";
                        }
                        if($scope.presentAccountInfo.organizationId!=null){
                            GetOrganizationInfo.get({id:$scope.presentAccountInfo.organizationId},function(result){
                                $rootScope.currentOrganization = result;
                            })
                        }
                        newMessageManage.query(function(result){
                            $scope.message=result;
                            angular.forEach($scope.message,function(data,index,array){
                                if(!data.isRead){
                                    $scope.MessageNoReply=$scope.MessageNoReply+1;
                                }
                            });
                            //console.log($scope.MessageNoReply);
                        });
                    },
                    function(result){
                    });
            }



        });
        $scope.LookMessage=function(){
            $state.go("messagePersonalList");
        };


        //菜单栏控制
        $scope.style1="dropdown";
        $scope.style2="dropdown";
        $scope.style3="dropdown user";
        $scope.enter = function(option){
            // console.log("enter " + option);
            if(option==1){
                $scope.style1 = "dropdown open";
            }
            if(option==2){
                $scope.style2 = "dropdown open";
            }
            if(option==3){
                $scope.style3 = "dropdown user open";
            }
        };
        $scope.leave = function(option){
            // console.log("leave " + option);
            if(option==1){
                $scope.style1="dropdown";
            }
            if(option==2){
                $scope.style2="dropdown";
            }
            if(option==3){
                $scope.style3="dropdown user";
            }
        }


    });

